package com.qianke.core.service.impl;

import com.qianke.common.CommonUtils;
import com.qianke.common.RequestContext;
import com.qianke.core.mapper.AdminUserMapper;
import com.qianke.core.mapper.AdminUserRoleRMapper;
import com.qianke.core.service.AdminUserService;
import com.qianke.domain.dto.AdminUserDto;
import com.qianke.domain.entity.AdminUser;
import com.qianke.domain.entity.AdminUserRoleR;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;
    @Resource
    private AdminUserRoleRMapper adminUserRoleRMapper;

    @Override
    public AdminUserDto queryAdminUserByAccount(String account) {
        return adminUserMapper.selectByAccount(account);
    }

    @Override
    public int insert(AdminUserDto adminUserDto) {
        AdminUser adminUser = new AdminUser();
        BeanUtils.copyProperties(adminUserDto, adminUser);
        CommonUtils.insert(adminUser, RequestContext.getLoginUser());
        adminUserMapper.insertSelective(adminUser);
        AdminUserRoleR adminUserRoleR = new AdminUserRoleR();
        adminUserRoleR.setUid(adminUser.getId());
        adminUserRoleR.setRoleId(adminUserDto.getRoleId());
        CommonUtils.insert(adminUserRoleR, RequestContext.getLoginUser());
        return adminUserRoleRMapper.insertSelective(adminUserRoleR);
    }

    @Override
    public int update(AdminUserDto adminUserDto) {
        AdminUser adminUser = new AdminUser();
        BeanUtils.copyProperties(adminUserDto, adminUser);
        CommonUtils.update(adminUser, RequestContext.getLoginUser());
        adminUserMapper.updateByPrimaryKeySelective(adminUser);
        AdminUserRoleR adminUserRoleR = new AdminUserRoleR();
        adminUserRoleR.setUid(adminUser.getId());
        adminUserRoleR.setRoleId(adminUserDto.getRoleId());
        adminUserRoleR.setStatus(adminUserDto.getStatus());
        CommonUtils.update(adminUserRoleR, RequestContext.getLoginUser());
        adminUserRoleRMapper.updateByPrimaryKeySelective(adminUserRoleR);
        return adminUserRoleRMapper.updateByPrimaryKeySelective(adminUserRoleR);
    }

}
